package main.util;

import org.springframework.data.domain.Sort;

/**
 * 排序封装类
 * @author wfy
 *
 */
public class SortTool {

	/**
	 * 默认按照id字段desc排序
	 * @return
	 */
	public static Sort basicSort() {
		
		return basicSort("desc","id");
	}
	
	/**
	 * 手动传入排序类型和排序字段
	 * @param orderType
	 * @param orderField
	 * @return
	 */
	public static Sort basicSort(String orderType,String orderField) {
		
		Sort sort = new Sort(Sort.Direction.fromString(orderType),orderField);
		return sort;
	}
	
	/**
	 * 按照排序对象来排序
	 * @param sortDtos
	 * @return
	 */
	public static Sort basicSort(SortDto...sortDtos) {
		
		Sort resultSort = null;
		for(int i = 0;i<sortDtos.length;i++) {
			SortDto sortDto = sortDtos[i];
			if (resultSort==null) {
				resultSort = new Sort(Sort.Direction.fromString(sortDto.getOrderType()),sortDto.getOrderField());
			} else {
				resultSort = resultSort.and(new Sort(Sort.Direction.fromString(sortDto.getOrderType()),sortDto.getOrderField()));
			}
		}
		return resultSort;
	}
}
